﻿//=============================================================
//  Copyright (C) 2016-2100
//  CLR版本:                	4.0.30319.42000
//  机器名称:               	YPFCOMPUTER
//  命名空间名称/文件名:    	SelfHostWebApi/CreateImageController 
//  创建人:			   	  		YPF     
//  创建时间:     		  		2017/5/9 10:16:23
//  网站：				  		
//==============================================================
using System;
using System.Web.Http;

namespace SelfHostWebApi
{
    public class CreateImageController : ApiController
    {

        [HttpGet]
        /// <summary>
        /// 生成截图
        /// </summary>
        /// <param name="uploadDir">服务器的Upload 文件夹 物理地址</param>
        /// <param name="oriVideoPath">视频全路径</param>
        /// <param name="numberOfFrames">帧数</param>
        /// <param name="width">截图宽</param>
        /// <param name="height">截图高</param>
        /// <returns>截图全路径</returns>
        public IHttpActionResult CatchThumb(string uploadDir, string oriVideoPath)
        {
            string[] imageUrls = new string[3];
            int[] timeAt = new int[3];
            var helper = new ImageHelper();
            string totalTime = "";
            var videoInfo = helper.GetVideoInfo(oriVideoPath, out totalTime);
            var t = Convert.ToDateTime(totalTime);
            int totalSeconds = 1;
            int r = 0;
            totalSeconds = Convert.ToInt32(new TimeSpan(t.Hour, t.Minute, t.Second).TotalSeconds);
            for (int i = 0; i < 3; i++)
            {
                Random random = new Random();
                r = random.Next(1, totalSeconds);
                string url = helper.CatchThumb(uploadDir, oriVideoPath, r, videoInfo.Width, videoInfo.Height);
                if (!string.IsNullOrEmpty(url))
                {
                    imageUrls[i] = url;
                    timeAt[i] = r;
                }
            }
            return Ok(new { Url = imageUrls, TimeAt = timeAt });
        }
        /// <summary>
        /// 获取视频信息
        /// </summary>
        /// <returns></returns>
        public IHttpActionResult Get(string oriVideoPath)
        {
            var helper = new ImageHelper();
            string totalTime = "";
            var videoInfo = helper.GetVideoInfo(oriVideoPath, out totalTime);
            return Ok(videoInfo);
        }
    }
}
